﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ATW.MT.BUS;
using ATW.MT.CommonClass;
using System.Text.RegularExpressions;
using System.Collections;

namespace ATW.MT.SellManage
{
    public partial class SysArgsManage : CommonPage
    {
        bus_D_SystemArgs SA = new bus_D_SystemArgs();
        bus_T_appuser au = new bus_T_appuser();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                loadUser();
            }
        }
        protected void BindData()
        {//读取
            try
            {
                DataTable dt = SA.SysQuery();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string key = dt.Rows[i]["key"].ToString();
                        string value = dt.Rows[i]["value"].ToString();
                        #region 取值
                        if (key == "ConcernGap")
                        {
                            txtConcernGap.Text = value;
                            hdfConcernGap.Value = value;
                        }
                        if (key == "ConcernPrice")
                        {
                            txtConcernPrice.Text = value;
                            hdfConcernPrice.Value = value;
                        }

                        if (key == "MinRate")
                        {
                            txtMinRate.Text = value;
                            hdfMinRate.Value = value;
                        }
                        if (key == "Version")
                        {
                            txtVersion.Text = value;
                            hdfVersion.Value = value;
                        }
                        if (key == "TimeDeviation")
                        {
                            txtTimeDeviation.Text = value;
                            hdfTimeDeviation.Value = value;
                        }
                        if (key == "PriceDeviation")
                        {
                            txtPriceDeviation.Text = value;
                            hdfPriceDeviation.Value = value;
                        }
                        if (key == "CustomerRate")
                        {
                            txtCustomerRate.Text = value;
                            hdfCustomerRate.Value = value;
                        }
                        //if (key == "ConcernCheck")
                        //{
                        //    txtConcernCheck.Text = value;
                        //    hdfConcernCheck.Value = value;
                        //}
                        if (key == "RequestTTL")
                        {
                            txtRequestTTL.Text = value;
                            hdfRequestTTL.Value = value;
                        }
                        if (key == "MaxRate")
                        {
                            txtMaxRate.Text = value;
                            hdfMaxRate.Value = value;
                        }
                        if (key == "ResponseTTL")
                        {
                            txtResponseTTL.Text = value;
                            hdfResponseTTL.Value = value;
                        }
                        if (key == "MaxDeviation")
                        {
                            txtMaxDeviation.Text = value;
                            hdfMaxDeviation.Value = value;
                        }
                        if (key == "PrePayLimit")
                        {
                            txtPrePayLimit.Text = value;
                            hdfPrePayLimit.Value = value;
                        }
                        if (key == "SumPrice")
                        {
                            txtSumPrice.Text = value;
                            hdfSumPrice.Value = value;
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {//修改
            try
            {
                #region 取值
                string ReqTTL = txtRequestTTL.Text.Trim();
                string CR = txtCustomerRate.Text.Trim();
                string MinRate = txtMinRate.Text.Trim();
                string MaxRate = txtMaxRate.Text.Trim();
                string ResTTL = txtResponseTTL.Text.Trim();

                string TD = txtTimeDeviation.Text.Trim();
                string PD = txtPriceDeviation.Text.Trim();

                string version = txtVersion.Text.Trim();
                string CG = txtConcernGap.Text.Trim();

                string CP = txtConcernPrice.Text.Trim();
                //string CC = txtConcernCheck.Text.Trim();
                string MD = txtMaxDeviation.Text.Trim();
                string PPL = txtPrePayLimit.Text.Trim();
                string SP = txtSumPrice.Text.Trim();
                #endregion
                #region 校验
                if (ReqTTL == "")
                {
                    Tips("询价有效期不能为空！");
                    txtRequestTTL.Focus();
                    return;
                }
                if (!common.isInt(ReqTTL) || int.Parse(ReqTTL) <= 0)
                {
                    Tips("询价有效期应当为正整数");
                    txtRequestTTL.Focus();
                    return;
                }
                if (CR == "")
                {
                    Tips("客户默认价格系数不能为空！");
                    txtCustomerRate.Focus();
                    return;
                }
                if (!common.isFloat(CR) || float.Parse(CR) <= 0)
                {
                    Tips("客户默认系数应当为正实数！");
                    txtCustomerRate.Focus();
                    return;
                }
                if (MinRate == "")
                {
                    Tips("价格系数可调范围最小值不能为空！");
                    txtMinRate.Focus();
                    return;
                }
                if (!common.isFloat(MinRate) || float.Parse(MinRate) <= 0)
                {
                    Tips("价格系数最小值应当为正实数！");
                    txtMinRate.Focus();
                    return;
                }
                if (MaxRate == "")
                {
                    Tips("价格系数可调范围最大小值不能为空！");
                    txtMaxRate.Focus();
                    return;
                }
                if (!common.isFloat(MaxRate) || float.Parse(MaxRate) <= 0)
                {
                    Tips("价格系数最大值应当为正实数！");
                    txtMaxRate.Focus();
                    return;
                }
                if (ResTTL == "")
                {
                    Tips("反馈报价时间不能为空！");
                    txtResponseTTL.Focus();
                    return;
                }
                if (!common.isInt(ResTTL) || int.Parse(ResTTL) <= 0)
                {
                    Tips("反馈报价时间应当为正整数！");
                    txtResponseTTL.Focus();
                    return;
                }
                if (TD == "")
                {
                    Tips("交货期偏离值不能为空！");
                    txtTimeDeviation.Focus();
                    return;
                }
                if (!common.isFloat(TD) || float.Parse(TD) <= 0)
                {
                    Tips("交货期偏离值应当为正实数！");
                    txtTimeDeviation.Focus();
                    return;
                }
                if (PD == "")
                {
                    Tips("无效价格偏离值不能为空！");
                    txtPriceDeviation.Focus();
                    return;
                }
                if (!common.isFloat(PD) || float.Parse(PD) <= 0)
                {
                    Tips("无效价格偏离值应当为正实数！");
                    txtPriceDeviation.Focus();
                    return;
                }

                if (version == "")
                {
                    Tips("版本号不能为空！");
                    txtVersion.Focus();
                    return;
                }
                //if (!Regex.IsMatch(version, @"^\d+[.]\d+[.]\d+$"))
                //{
                //    Tips("版本号格式不正确！");
                //    txtVersion.Focus();
                //    return;
                //}
                if (SP == "")
                {
                    Tips("累计关怀金额不能为空！");
                    return;
                }
                int _sp=0;
                if (!int.TryParse(SP, out _sp) || _sp <= 0)
                {
                    Tips("累计关怀间隔应当为正整数！");
                    return;
                }
                if (CG == "")
                {
                    Tips("关怀间隔时间不能为空！");
                    txtConcernGap.Focus();
                    return;
                }
                if (!common.isInt(CG) || int.Parse(CG) <= 0)
                {
                    Tips("关怀间隔时间应当为正整数！");
                    txtConcernGap.Focus();
                    return;
                }
                if (CP == "")
                {
                    Tips("关怀提醒金额不能为空！");
                    txtConcernPrice.Focus();
                    return;
                }
                if (!common.isFloat(CP) || float.Parse(CP) <= 0)
                {
                    Tips("关怀提醒金额应当为正实数！");
                    txtConcernPrice.Focus();
                    return;
                }
                //if (CC == "")
                //{
                //    Tips("检查关怀时间不能为空！");
                //    txtConcernCheck.Focus();
                //    return;
                //}
                //if (!common.isInt(CC) || float.Parse(CC) <= 0)
                //{
                //    Tips("检查关怀时间应当为正整数！");
                //    txtConcernCheck.Focus();
                //    return;
                //}
                if (PPL == "")
                {
                    Tips("不需预付款的合同金额上限不能为空！");
                    txtPrePayLimit.Focus();
                    return;
                }
                decimal _ppl = 0;
                if (!decimal.TryParse(PPL, out _ppl) || _ppl < 0)
                {
                    Tips("不需预付款的合同金额上限应当为数字");
                    return;
                }
                #endregion
                ArrayList al = new ArrayList();
                #region 拼接

                if (ReqTTL != hdfRequestTTL.Value)
                {
                    al.Add(SA.GetUpdateString("RequestTTL", ReqTTL));
                }
                if (CR != hdfCustomerRate.Value)
                {
                    al.Add(SA.GetUpdateString("CustomerRate", CR));
                }
                if (MinRate != hdfMinRate.Value)
                {
                    al.Add(SA.GetUpdateString("MinRate", MinRate));
                }
                if (MaxRate != hdfMaxRate.Value)
                {
                    al.Add(SA.GetUpdateString("MaxRate", MaxRate));
                }
                if (ResTTL != hdfResponseTTL.Value)
                {
                    al.Add(SA.GetUpdateString("ResponseTTL", ResTTL));
                }
                if (TD != hdfTimeDeviation.Value)
                {
                    al.Add(SA.GetUpdateString("TimeDeviation", TD));
                }
                if (PD != hdfPriceDeviation.Value)
                {
                    al.Add(SA.GetUpdateString("PriceDeviation", PD));
                }

                if (version != hdfVersion.Value)
                {
                    al.Add(SA.GetUpdateString("Version", version));
                }
                if (CG != hdfConcernGap.Value)
                {
                    al.Add(SA.GetUpdateString("ConcernGap", CG));
                }
                if (CP != hdfConcernPrice.Value)
                {
                    al.Add(SA.GetUpdateString("ConcernPrice", CP));
                }
                //if (CC != hdfConcernCheck.Value)
                //{
                //    al.Add(SA.GetUpdateString("ConcernCheck", CC));
                //}
                if (MD != hdfMaxDeviation.Value)
                {
                    al.Add(SA.GetUpdateString("MaxDeviation", MD));
                }
                if (PPL != hdfPrePayLimit.Value) ;
                {
                    al.Add(SA.GetUpdateString("PrePayLimit", PPL));
                }
                if (SP != hdfSumPrice.Value)
                {
                    al.Add(SA.GetUpdateString("SumPrice", SP));
                }
                #endregion
                bool result = SA.ExAL(al);
                if (result)
                {
                    Tips("修改成功！");
                    BindData();
                }
                else
                {
                    Tips("修改失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnSelectUser_Click(object sender, EventArgs e)
        {//展开
            LinkButton lbn = (LinkButton)sender;
            divMail.Visible = true;
            lbnSaveUser.Visible = true;
            lbn.Visible = false;
        }
        protected void lbnSaveUser_Click(object sender, EventArgs e)
        {//保存
            LinkButton lbn = (LinkButton)sender;
            divMail.Visible = false;
            lbnSaveUser.Visible = false;
            try
            {
                string mail = txtMail.Text.Trim();
                if (mail == "")
                {
                    Tips("请填写邮箱！");
                    return;
                }
                int result = SA.addUser(mail);
                if (result > 0)
                {
                    Tips("新增成功！");
                    loadUser();
                    txtMail.Text = "";
                    lbnSelectUser.Visible = true;
                }
                else
                {
                    Tips("新增失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnDelUser_Click(object sender, EventArgs e)
        {
            LinkButton lbn = (LinkButton)sender;
            string sid = lbn.CommandArgument;
            try
            {
                int result = SA.delUser(sid);
                if (result > 0)
                {
                    Tips("删除成功！");
                    loadUser();
                }
                else
                {
                    Tips("删除失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void loadUser()//加载推送用户列表
        {
            try
            {
                DataTable dt = SA.getUserList();
                rpUser.DataSource = dt;
                rpUser.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }//提示

    }
}